package gbench.sandbox.matrix.img;

import org.junit.jupiter.api.Test;

import java.util.*;

import gbench.common.matlib.img.ImageOps;
import gbench.sandbox.matrix.img.util.MatlibCanvas;
import static gbench.sandbox.matrix.img.util.ImageOps.*;
import static gbench.sandbox.matrix.img.util.MatlibCanvas.*;

import static gbench.common.tree.LittleTree.IRecord.*;
import static gbench.common.tree.LittleTree.*;
import static java.lang.Math.*;

/**
 * 简单的绘图
 * @author gbench
 *
 */
public class JunitPlotRhoTheta {
    
    @Test
    public void bar() {
        final var size = 1000;
        final var matlib = new MatlibCanvas(size);
        final var data = REC(
            "一月销售",rndint(1000),
            "二月销售",rndint(1000),
            "三月销售",rndint(1000),
            "四月销售",rndint(1000),
            "五月销售",rndint(1000),
            "六月销售",rndint(1000)
        );// 源数据
        
        final var sum = data.collect(dbl_stats2(kv2dbl2)).getSum();
        
        matlib.plot(size, size / 2, pen -> {
            final var thetas = data.stream(kv2dbl2).map(e->e/sum*2*PI).sorted()
                .reduce(new LinkedList<Double>(),
                    (aa,a)->{aa.add((aa.size()<1?0d:aa.getLast())+a);return aa;},
                    (aa,bb)->{aa.addAll(bb);return aa;});// 获得区间的累积数据
            
            // 先前的角度
            thetas.stream().map(kvp_int()).forEach( tup -> {
                final var start = tup._1()==0 ? 0 : thetas.get(tup._1()-1);// 起点角度
                final var end = tup._2();// 终点角度
                final var clr = ImageOps.rndrgb();
                final var points = Range.cph(
                        Range.of(100,200).limit(200), // 向量长度,精度200
                        Range.of(start, end).limit((end-start)*(2000/(2*PI))) // 角度范围,精度一周2000
                    ).map(vec->matlib.rhoTheta(vec.get(0), vec.get(1))).map(vec->{
                        vec.idval_set(2,clr[0],3,clr[1],4,clr[2]);
                        return vec;
                    }).map(e->e.affine_offset(300,210)); // 平移到原点
                
                // 绘制点向量
                pen.drawPoints(points);
            }); // forEach 
        },true);// plot
    }
    
    public static void main(String[] args) {
        //new JunitPlot().foo();
        new JunitPlotRhoTheta().bar();
    }

}
